using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.WebControls;
using System.Web.UI;

namespace PhilWicklund.com.FacebookParts
{
    public class ProfilePictureEditorPart : EditorPart
    {
        CheckBox useLoggedInUser;
        TextBox userId;
        DropDownList sizes;
        DropDownList orientation;
        
        protected override void CreateChildControls()
        {
            Controls.Add(new LiteralControl("Use Logged In User:<br />"));
            useLoggedInUser = new CheckBox();
            this.Controls.Add(useLoggedInUser);

            this.Controls.Add(new LiteralControl("<br /><br />"));

            Controls.Add(new LiteralControl("Facebook User Id:<br />"));
            userId = new TextBox();
            this.Controls.Add(userId);

            this.Controls.Add(new LiteralControl("<br /><br />"));

            Controls.Add(new LiteralControl("Picture Size:<br />"));
            sizes = new DropDownList();
            sizes.Items.Add(Sizes.thumb.ToString());
            sizes.Items.Add(Sizes.small.ToString());
            sizes.Items.Add(Sizes.normal.ToString());
            this.Controls.Add(sizes);

            this.Controls.Add(new LiteralControl("<br /><br />"));

            Controls.Add(new LiteralControl("Orientation:<br />"));
            orientation = new DropDownList();
            orientation.Items.Add(Orientations.left.ToString());
            orientation.Items.Add(Orientations.right.ToString());
            orientation.Items.Add(Orientations.center.ToString());
            this.Controls.Add(orientation);
        }

        public override void SyncChanges()
        {
            this.EnsureChildControls();
            ProfilePicturePart sourcePart = (ProfilePicturePart)this.WebPartToEdit;
            string SelectedSize = sourcePart.ImageSize.ToString();
            bool UseLoggedInUser = sourcePart.UseLoggedInUser;
            int id = sourcePart.UserId;

            sizes.Items.FindByText(SelectedSize).Selected = true;
            useLoggedInUser.Checked = UseLoggedInUser;
            userId.Text = id.ToString();

            string SelectedOrientation = sourcePart.Orientation.ToString();
            orientation.Items.FindByText(SelectedOrientation).Selected = true;
        }

        public override bool ApplyChanges()
        {
            this.EnsureChildControls();
            ProfilePicturePart targetPart = (ProfilePicturePart)this.WebPartToEdit;
            switch (sizes.SelectedItem.Text)
            {
                case "thumb": targetPart.ImageSize = Sizes.thumb; break;
                case "small": targetPart.ImageSize = Sizes.small; break;
                case "normal": targetPart.ImageSize = Sizes.normal; break;
            }

            targetPart.UseLoggedInUser = useLoggedInUser.Checked;
            try
            {
                targetPart.UserId = int.Parse(userId.Text);
            }
            catch { throw new Exception("ERROR: you must type only numbers in the User ID text box."); }

            switch (orientation.SelectedItem.Text)
            {
                case "left": targetPart.Orientation = Orientations.left; break;
                case "right": targetPart.Orientation = Orientations.right; break;
                case "center": targetPart.Orientation = Orientations.center; break;
            }
            return true;
        }
    }
}
